package com.sanhe.rfmuscle.activity.work;

import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.menu.app.MyApp;
import com.menu.app.util.Utils;
import com.sanhe.rfmuscle.App;
import com.sanhe.rfmuscle.R;
import com.sanhe.rfmuscle.databinding.ActivitySnBinding;

import me.drakeet.support.toast.ToastUtil;

public class SnActivity extends BaseWorkActivity {
    Typeface medium = Typeface.createFromAsset(App.getApp().getAssets(), "HarmonyOS_Sans_Medium.ttf");
    ActivitySnBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySnBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.title.setTypeface(medium);

        binding.back.setOnClickListener(v -> finish());
        binding.ok.setOnClickListener(v -> {
            commitSn();

        });
        binding.snEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && actionId == EditorInfo.IME_ACTION_UNSPECIFIED && event.getAction() == KeyEvent.ACTION_DOWN) || actionId == EditorInfo.IME_ACTION_DONE
                        || (event != null
                        && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event
                        .getAction() == KeyEvent.ACTION_DOWN)) {

                    commitSn();

                    return true;
                }
                return false;
            }
        });


        binding.ok.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    binding.okBg.setImageResource(R.mipmap.shade_bg_press);
                }else if (event.getAction() == MotionEvent.ACTION_UP) {
                    binding.okBg.setImageResource(R.mipmap.shade_bg);
                }
                return false;
            }
        });


        updateSn();
    }

    private void commitSn() {
        Utils.hideInput(binding.snEt);
        String s = binding.snEt.getText().toString().trim();
        Log.e(TAG, "sn: " + s);
        if (TextUtils.isEmpty(s)) {
            ToastUtil.showShortToast(MyApp.getApp(), "Enter Sn");
            return;
        }
        Utils.setMsn(s);
        updateSn();
    }

    private void updateSn() {
        String s = Utils.getMsn();
        if ("none".equals(s)) {
            s = "";
        }
        binding.snTv.setText(s);
    }

}
